﻿// Spun AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Formality.FormalSystem
{
    public class Substitution : IEnumerable<Pair<Variable, Proposition>>
    {
        public Substitution()
        {
            Map = new Dictionary<Variable, Proposition>();
        }

        public Substitution(Substitution Base)
        {
            Map = new Dictionary<Variable, Proposition>(Base.Map);
        }

        public Proposition this[Variable Variable]
        {
            get { return Map[Variable]; }
            set { Map[Variable] = value; }
        }

        public bool HasSubstitution(Variable Variable)
        {
            return Map.ContainsKey(Variable);
        }

        public void RemoveSubstitution(Variable Variable)
        {
            Map.Remove(Variable);
        }

        public override string ToString()
        {
            var VariableBuffer = new List<string>();
            var ReplacementBuffer = new List<string>();

            foreach (var Sub in Map)
            {
                VariableBuffer.Add(Sub.Key.ToString());
                ReplacementBuffer.Add(Sub.Value.ToString());
            }

            return string.Format("[{0}/{1}]", VariableBuffer.Join(","), ReplacementBuffer.Join(","));
        }

        private Dictionary<Variable, Proposition> Map;

        public IEnumerator<Pair<Variable, Proposition>> GetEnumerator()
        {
            foreach (var Sub in Map)
                yield return Pair.New(Sub.Key, Sub.Value);
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
    }
}
